package cc.ttiot.thread.dataTransmissionThread;

import cc.ttiot.mapper.dto.Picture;
import cc.ttiot.service.dataTransmission.mqtt.Client;
import org.eclipse.paho.client.mqttv3.MqttException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import static cc.ttiot.enums.SystemConstant.CAMERA;
import static cc.ttiot.service.dataTransmission.DataTransmission.transmit;
import static cc.ttiot.utils.JavaBase64Utils.encode2String;
import static cc.ttiot.utils.TimeUtils.getMillisecondTime;

/**
 * @Description: TODO
 * @Author WangSuheng
 * @Date 2021/11/22 1:19
 * @Version 1.0
 */
public class PictureTransmitThread implements Runnable {

    @Override
    public void run() {
        Client client = Client.getInstance();

        //初始化对象用于存储信息
        Picture picture = new Picture();

        while (true) {
            //TODO 客户端始终保持连接状态的情况，考虑能不能优化连接，降低服务器的压力
            if (!client.isConnected()) {
                try {
                    client.connect();
                } catch (MqttException e) {
                    e.printStackTrace();
                }
            }

            //图片转化为二进制
            byte[] imageBytes = null;
            File file = new File("/home/pi/ProtectiveDoor/door.png");

            if (file.exists()) {
                try {
                    FileInputStream fileInputStream = new FileInputStream(file);
                    imageBytes = new byte[fileInputStream.available()];
                    fileInputStream.read(imageBytes);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                picture.setPicture(encode2String(imageBytes));
                picture.setTime(getMillisecondTime());

                try {
                    transmit(CAMERA, picture);
                } catch (MqttException e) {
                    e.printStackTrace();
                }
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

    }

    public static void main(String[] args) {
        Client client = Client.getInstance();
        try {
            client.connect();
        } catch (MqttException e) {
            e.printStackTrace();
        }

        Picture picture = new Picture();

        //图片转化为二进制
        byte[] imageBytes = null;
        File file = new File("C:\\Users\\WangSuheng\\Desktop\\te1\\test1.png");
        while(true){
            if (file.exists()) {
                try {
                    FileInputStream fileInputStream = new FileInputStream(file);
                    imageBytes = new byte[fileInputStream.available()];
                    fileInputStream.read(imageBytes);
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println(imageBytes);

                picture.setPicture(encode2String(imageBytes));
                picture.setTime(getMillisecondTime());

                try {
                    transmit(CAMERA, picture);
                } catch (MqttException e) {
                    e.printStackTrace();
                }
                System.out.println("end");

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }


//        try {
//            client.disconnect();
//        } catch (MqttException e) {
//            e.printStackTrace();
//        }

    }
}
